package com.fesine.ssh.controller;

import com.fesine.ssh.model.SaleItem;
import com.fesine.ssh.model.SalesOrder;
import com.fesine.ssh.service.SalesOrderService;
import com.fesine.ssh.service.SequenceService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.List;
import java.util.Set;


@Controller
@RequestMapping("/salesOrder")
public class SalesOrderController {
    private static final Logger logger = Logger.getLogger(SalesOrderController.class);

    @Autowired
    private SalesOrderService salesOrderService;
    @Autowired
    private SequenceService sequenceService;


    /**
     * 定单列表
     */
    @RequestMapping(value = "/listSalesOrder")
    public void listSalesOrder(HttpServletRequest req, PrintWriter writer) {
        String offset = req.getParameter("start");
        String pageSize = req.getParameter("limit");
        int len = salesOrderService.findAll().size();
        List<SalesOrder> orders =  salesOrderService.findAll(Integer.parseInt(offset), Integer.parseInt(pageSize));
        String preStr = "{totalCount:" + len + ",rows:[";
        String cenStr = "";
        int i = 0;
        String douhao = ",";
        for (SalesOrder order : orders) {
            double totalPrice = order.getTotalPrice();
            //Set<SaleItem> items = order.getSaleItems();

            i++;
            cenStr += "{username:'" + order.getUsername() + "', " +
                    "id:'" + order.getId() + "'," +
                    "orderId:'" + order.getOrderId() + "'," +
                    "status:'" + order.getStatus() + "'," +
                    "nullify:'" + order.getNullify() + "'," +
                    "ordDate:'" + order.getOrdDate() + "'," +
                    "phone:'" + order.getPhone() + "'," +
                    "qq:'" + order.getQq() + "'," +
                    "address:'" + order.getAddress() + "'," +
                    "totalmoney:'" + Math.round(totalPrice * 100) / 100.0 + "'," +
                    "remark:'" + order.getRemark() + "'" +
                    "}";
            if (i < len) {
                cenStr += douhao;
            }
        }
        String endStr = "]}";
        String resultStr = preStr + cenStr + endStr;
        writer.write(resultStr);
    }

    /**
     * 根据orderId显示定单详细信息
     */
    @RequestMapping("/showSalesOrderDetail")
    public void showSalesOrderDetail(HttpServletRequest request, PrintWriter writer) {
        String orderId = request.getParameter("orderId");

        SalesOrder salesOrder = salesOrderService.findByOrderId(orderId);
        int len = salesOrder.getSaleItems().size();

        Set<SaleItem> items = salesOrder.getSaleItems();

        String preStr = "{totalCount:" + items.size() + ",rows:[";
        String cenStr = "";
        int i = 1;
        String douhao = ",";
        for (SaleItem item : items) {

            cenStr += "{" +
                    "id:'" + item.getId() + "', " +
                    "orderId:'" + orderId + "'," +
                    "prodId:'" + item.getProdId() + "', " +
                    "prodName:'" + item.getProdName() + "'," +
                    "count:'" + item.getNumber() + "'," +
                    "price:'" + item.getPrice() + "'," +
                    "sum:'" + Math.round((item.getNumber() * item.getPrice()) * 100) / 100.0 + "'" +
                    "}";
            if (i < len) {
                cenStr += douhao;
            }
        }
        String endStr = "]}";
        String resultStr = preStr + cenStr + endStr;
        writer.write(resultStr);
    }

    /**
     * 审核定单
     */
    @RequestMapping("/checkSalesOrder")
    public void checkSalesOrderDetail(HttpServletRequest request, PrintWriter writer) {
        String orderId = request.getParameter("orderId");
        String[] orderIdArr = orderId.split(",");
        salesOrderService.verify(orderIdArr);
        writer.write("{success:true, msg:'审核成功!'}");
    }

    /**
     * 作废定单
     */
    @RequestMapping("/removeOrder")
    public void removeOrder(HttpServletRequest request, PrintWriter writer) {
        String orderId = request.getParameter("orderId");
        String[] orderIdArr = orderId.split(",");
        salesOrderService.delete(orderIdArr);
        writer.write("{success:true, msg:'作废成功!'}");
    }
}
